package com.joe.algorithm.hash;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashSet;

import com.joe.utils.io.GetString;

public class HashTest {

	public HashSet hashSet = null;
	
	public HashTest(){
		hashSet = new HashSet();
		loadTestData(hashSet,"testdata/MD50000.TXT");
	}
	
	public void loadTestData(HashSet hashSet,String filePath){
		long start = System.currentTimeMillis();
		String dataline;
		String temp = "";
		try {
			/**
			FC0E375F	5100000176881664	A
			F6C7EB69	5100000182085117	A
			4A593882	5100000189237685	A
			 */
			InputStream setdata = getClass().getResourceAsStream(filePath);
			BufferedReader in = new BufferedReader(new InputStreamReader(
					setdata, "UTF-8"));
			while ((dataline = in.readLine()) != null) {
				if ((dataline.length() == 0)) {
					continue;
				}
				temp = dataline.split("\t")[0];
				if(!hashSet.contains(temp)){
					hashSet.add(temp);
				}
			}
			in.close();
		} catch (Exception e) {
			System.err.println("Exception loading data file" + filePath + " " + e);
		} finally {
			long end = System.currentTimeMillis();
			System.out.println("loaded " + hashSet.size() + " items in the HashSet! cost : "+(end-start)+" ms.");
		}
	}
	
	public void searchByHash(String str){
		long start = System.currentTimeMillis();
		if(hashSet.contains(str)){
			System.err.println("contains this card No. : "+str);
		}else{
			System.out.println("not contains this card No. : "+str);
		}
		long end = System.currentTimeMillis();
		System.out.println("find the card No. '"+str+"' cost : "+ (end-start)+" ms.");
	}
	
	public void run(){
		String str = null;
		while(!(str=GetString.getString()).equals("end")){
			searchByHash(str);
		}
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		HashTest ht = new HashTest();
		//System.out.println(ht.hashSet.size());
		//System.out.println(ht.hashSet.iterator().next()+","+ht.hashSet.iterator().next());
		ht.run();
	}

}
